import 'package:flutter/material.dart';

class TextFieldPage extends StatefulWidget {
  TextFieldPage({Key key}) : super(key: key);

  @override
  _TextFieldPageState createState() => _TextFieldPageState();
}

class _TextFieldPageState extends State<TextFieldPage> {
  var name = new TextEditingController();
  var password;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    name.text = '初始值';
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("TextField"),
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        //  child: TextFieDemo()
        child: Column(
          children: <Widget>[
            TextField(
              decoration: InputDecoration(hintText: "请输入用户名"),
              controller: this.name,
              onChanged: (value) {
                setState(() {
                  name.text = value;
                });
              },
            ),
            SizedBox(height: 20),
            TextField(
              decoration: InputDecoration(hintText: "请输入密码"),
              obscureText: true,
              onChanged: (value) {
                setState(() {
                  this.password = value;
                });
              },
            ),
            SizedBox(height: 20),
            Container(
              width: double.infinity,
              height: 40,
              child: RaisedButton(
                child: Text("登录"),
                onPressed: () {
                  print(this.name.text);
                  print(this.password);
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}

class TextFieDemo extends StatefulWidget {
  TextFieDemo({Key key}) : super(key: key);

  @override
  _TextFieDemoState createState() => _TextFieDemoState();
}

class _TextFieDemoState extends State<TextFieDemo> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          TextField(),
          SizedBox(
            height: 20,
          ),
          TextField(
            obscureText: true, //密码框
            maxLength: 4, //多行文本
            decoration: InputDecoration(
                hintText: "请输入搜索内容", border: OutlineInputBorder()),
          ),
          SizedBox(height: 20),
          TextField(
            obscureText: true, //密码框
            decoration:
                InputDecoration(labelText: "密码", border: OutlineInputBorder()),
          ),
          SizedBox(height: 20),
          TextField(
            decoration:
                InputDecoration(labelText: "用户名", border: OutlineInputBorder()),
          ),
          SizedBox(height: 20),
          TextField(
            decoration:
                InputDecoration(icon: Icon(Icons.people), hintText: "请输入用户名"),
          )
        ],
      ),
    );
  }
}
